<?php
namespace app\model;


class Like {

    static function getRealNum($uniacid,$themeid,$openid){
        $sql = "select count(*) from (SELECT COUNT(*) FROM ".tablename(t_like)."
    				WHERE uniacid = :uniacid and themeid = :themeid and fopenid = :openid and status = 1 group by openid) a";
        $lightsum = intval(db_fetchcolumn($sql,array(":uniacid"=>$uniacid,":themeid"=>$themeid,":openid"=>$openid)));
        return $lightsum;
    }

    static function getLogNum($id){
        $sql1 = "SELECT COUNT(*) FROM " . tablename(t_like_log) . "  WHERE likeid = '{$id}'";
        $nums = intval(db_fetchcolumn($sql1));
        return $nums;
    }

    static function getShowList($themeid,$shownum){
        global $_G, $_P;
        // $cachekey = 'show'.$_G['mpid'].MODULE_NAME.$themeid;
        // $settings = cache_load($cachekey);
        // if(empty($settings)){
            $p =  array(":themeid"=>$themeid);
            $settings = db_select("SELECT * FROM ".tablename(t_like)." as f
				WHERE themeid =:themeid and status != 0 group by openid order by id desc limit ".$shownum,$p);
        //     if(!empty($settings)){
        //         cache_write($cachekey, $settings);
        //     }
        // }
        return $settings;
    }

    static function getListByThemeId($themeid){
        if (empty($themeid)) {
            return false;
        }
        $p = array(":themeid"=>$themeid);
        $likelist = db_select("SELECT id,nickname,avatar FROM ".tablename(t_like)." as f
				WHERE themeid =:themeid and status != 0 order by id desc",$p);
        return $likelist;
    }


    static function addGoods($openid,$goodsid,$shopid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_goods)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$goodsid));
        $sql = "SELECT * FROM " . tablename(t_like) . " where openid = :openid and goodsid =:goodsid and type ='goods'";
        $row = db_find($sql, array(":openid"=>$openid,":goodsid"=>$goodsid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'goodsid'   => $goodsid,
                'shopid'    => $shopid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'goods',
            );
            $insertid = db_insert(t_like, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_like,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

    static function getNumByThemeIdAndOpenidAndStatus($themeid,$openid,$status=0) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "select count(*) from (SELECT COUNT(*) FROM ".tablename(t_like)."
    				WHERE themeid = :themeid and fopenid = :openid and status = :status group by openid) a";
        $row = db_fetchcolumn($sql, array(":themeid"=>$themeid,":openid"=>$openid,":status"=>$status));
        return $row;
    }

    static function addShop($openid,$shopid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_shop)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$shopid));
        $sql = "SELECT * FROM " . tablename(t_like) . " where openid = :openid and shopid =:shopid and type = 'shop'";
        $row = db_find($sql, array(":openid"=>$openid,":shopid"=>$shopid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'shopid'    => $shopid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'shop',
            );
            $insertid = db_insert(t_like, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_like,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

    static function addAgent($openid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_agent)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$agentid));
        $sql = "SELECT * FROM " . tablename(t_like) . " where openid = :openid and agentid =:agentid and type = 'agent'";
        $row = db_find($sql, array(":openid"=>$openid,":agentid"=>$agentid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'agent',
            );
            $insertid = db_insert(t_like, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_like,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

}
?>